/*  
 *  ------   [ZB_10] - scan network     -------- 
 *  
 *  Explanation: This program shows how to scan the XBee's network
 *  in order to find other XBee modules. This example prints all
 *  available data within the API structures
 *
 *  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    Yuri Carmona
 */

#include <MakeSureXBeeZB.h>

void setup()
{     
  // initSerial port
  Serial.begin(9600);
  Serial.println("ZB_10 example");
  
  //////////////////////////
  // 1. init XBee
  //////////////////////////
  xbeeZB.ON();  
  
  delay(3000);
  
  //////////////////////////
  // 2. check XBee's network parameters
  //////////////////////////
  checkNetworkParams();
  
}

void loop()
{ 
  ////////////////////////////////
  // 3. scan network
  ////////////////////////////////
  xbeeZB.scanNetwork(); 
  
  ////////////////////////////////
  // 4. print info
  ////////////////////////////////  

 Serial.print("\n\ntotalScannedBrothers:");
 Serial.println(xbeeZB.totalScannedBrothers,DEC); 

  // print all scanned nodes information
  printScanInfo();

}  


/*
 *  printScanInfo
 *
 *  This function prints all info related to the scan 
 *  process given by the XBee module
 */
void printScanInfo()
{  
 Serial.println(F("----------------------------"));

  for(int i=0; i<xbeeZB.totalScannedBrothers; i++)
  {  
   Serial.print("MAC:");
   Serial.print(xbeeZB.scannedBrothers[i].SH[0],HEX);	
   Serial.print(xbeeZB.scannedBrothers[i].SH[1],HEX);	
   Serial.print(xbeeZB.scannedBrothers[i].SH[2],HEX);	
   Serial.print(xbeeZB.scannedBrothers[i].SH[3],HEX);
   Serial.print(xbeeZB.scannedBrothers[i].SL[0],HEX);	
   Serial.print(xbeeZB.scannedBrothers[i].SL[1],HEX);	
   Serial.print(xbeeZB.scannedBrothers[i].SL[2],HEX);	
   Serial.print(xbeeZB.scannedBrothers[i].SL[3],HEX);

   Serial.print("\nNI:");
    for(int j=0; j<20; j++)
    {      
     Serial.print(xbeeZB.scannedBrothers[i].NI[j]);		
    } 

   Serial.print("\nDevice Type:");
    switch(xbeeZB.scannedBrothers[i].DT)
    {
    case 0: 
     Serial.print("Coordinator");
      break;
    case 1: 
     Serial.print("Router");
      break;
    case 2: 
     Serial.print("End Device");
      break;
    }

   Serial.print(F("\nPMY:"));
   Serial.print(xbeeZB.scannedBrothers[i].PMY[0],HEX);	
   Serial.print(xbeeZB.scannedBrothers[i].PMY[1],HEX);

   Serial.print(F("\nPID:"));
   Serial.print(xbeeZB.scannedBrothers[i].PID[0],HEX);	
   Serial.print(xbeeZB.scannedBrothers[i].PID[1],HEX);

   Serial.print(F("\nMID:"));
   Serial.print(xbeeZB.scannedBrothers[i].MID[0],HEX);	
   Serial.print(xbeeZB.scannedBrothers[i].MID[1],HEX);
    
   Serial.println("\n----------------------------");

  }
}





/*******************************************
 *
 *  checkNetworkParams - Check operating
 *  network parameters in the XBee module
 *
 *******************************************/
void checkNetworkParams()
{
  // 1. get operating 64-b PAN ID
  xbeeZB.getOperating64PAN();

  // 2. wait for association indication
  xbeeZB.getAssociationIndication();
 
  while( xbeeZB.associationIndication != 0 )
  { 
    delay(2000);
    
    // get operating 64-b PAN ID
    xbeeZB.getOperating64PAN();

   Serial.print(F("operating 64-b PAN ID: "));
   Serial.print(xbeeZB.operating64PAN[0],HEX);
   Serial.print(xbeeZB.operating64PAN[1],HEX);
   Serial.print(xbeeZB.operating64PAN[2],HEX);
   Serial.print(xbeeZB.operating64PAN[3],HEX);
   Serial.print(xbeeZB.operating64PAN[4],HEX);
   Serial.print(xbeeZB.operating64PAN[5],HEX);
   Serial.print(xbeeZB.operating64PAN[6],HEX);
   Serial.print(xbeeZB.operating64PAN[7],HEX);
   Serial.println();     
    
    xbeeZB.getAssociationIndication();
  }

 Serial.println(F("\nJoined a network!"));

  // 3. get network parameters 
  xbeeZB.getOperating16PAN();
  xbeeZB.getOperating64PAN();
  xbeeZB.getChannel();

 Serial.print(F("operating 16-b PAN ID: "));
 Serial.print(xbeeZB.operating16PAN[0],HEX);
 Serial.print(xbeeZB.operating16PAN[1],HEX);
 Serial.println();

 Serial.print(F("operating 64-b PAN ID: "));
 Serial.print(xbeeZB.operating64PAN[0],HEX);
 Serial.print(xbeeZB.operating64PAN[1],HEX);
 Serial.print(xbeeZB.operating64PAN[2],HEX);
 Serial.print(xbeeZB.operating64PAN[3],HEX);
 Serial.print(xbeeZB.operating64PAN[4],HEX);
 Serial.print(xbeeZB.operating64PAN[5],HEX);
 Serial.print(xbeeZB.operating64PAN[6],HEX);
 Serial.print(xbeeZB.operating64PAN[7],HEX);
 Serial.println();

 Serial.print(F("channel: "));
 Serial.print(xbeeZB.channel,HEX);
 Serial.println();

}





